
package edu.hm.devcamp.presentation.overlays;

import java.util.List;
import java.util.Set;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.provider.LiveFolders;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import edu.hm.devcamp.R;
import edu.hm.devcamp.presentation.LocationListenerImpl;
import edu.hm.devcamp.presentation.activities.PoiMap;
import edu.hm.devcamp.providers.PoiNoDataException;
import edu.hm.devcamp.providers.Poi;
import edu.hm.devcamp.providers.PoiProviderRequest;
import edu.hm.devcamp.providers.Pois.categories;

public class OverlayManager {

    Poi[] requestedList;

    PoiOverlay kulturzentren;

    PoiOverlay wertstoffhof;

    PoiOverlay wertstoffinsel;

    PoiOverlay toilette;

    PoiOverlay kinderspielplaetze;

    PoiOverlay taxistandplatz;

    PoiOverlay itemizedOverlay;

    List<Overlay> mapOverlays;

    private static OverlayManager instance;

    private Context mContext;

    private PoiOverlay playground;

    private PoiOverlay toilet;

    private PoiOverlay recycleSmall;

    private PoiOverlay cultureCenters;

    private PoiOverlay libraryOverlay;

    private PoiOverlay recyleCenter;

    private PoiOverlay taxiStop;

    private OverlayManager() {

    }

    private OverlayManager(Context context) {
        this.mContext = context;
        if (PoiMap.LOG_D) {
            Log.w("Sam", "creating overlayManager");
        }
        Poi[] emptyList = new Poi[0];

        Drawable defaultMarker = context.getResources().getDrawable(R.drawable.playground);
        playground = createOverlay(emptyList, defaultMarker);
        defaultMarker = context.getResources().getDrawable(R.drawable.toilet);
        toilet = createOverlay(emptyList, defaultMarker);
        defaultMarker = context.getResources().getDrawable(R.drawable.recycle_small);
        recycleSmall = createOverlay(emptyList, defaultMarker);
        defaultMarker = context.getResources().getDrawable(R.drawable.bavaria);
        cultureCenters = createOverlay(emptyList, defaultMarker);
        defaultMarker = context.getResources().getDrawable(R.drawable.library);
        libraryOverlay = createOverlay(emptyList, defaultMarker);
        defaultMarker = context.getResources().getDrawable(R.drawable.recycle_center);
        recyleCenter = createOverlay(emptyList, defaultMarker);
        defaultMarker = context.getResources().getDrawable(R.drawable.taxi);
        taxiStop = createOverlay(emptyList, defaultMarker);
    }

    public static OverlayManager getInstance(Context context) {

        if (instance == null) {
            instance = new OverlayManager(context);
        }

        return instance;
    }

    public void pullData() {

        if (PoiMap.LOG_D) {
            Log.w("Sam", "calling pullData");
        }

        requestedList = PoiProviderRequest.getList(mContext, categories.library);
        
        Drawable defaultMarker = mContext.getResources().getDrawable(R.drawable.library);
        libraryOverlay = createOverlay(requestedList, defaultMarker);

        requestedList = PoiProviderRequest.getList(mContext, categories.culturalCenter);
        
        Drawable defaultMarker1 = mContext.getResources().getDrawable(R.drawable.bavaria);
        cultureCenters = createOverlay(requestedList, defaultMarker1);

        requestedList = PoiProviderRequest.getList(mContext, categories.recycleSmall);
        
        Drawable defaultMarker2 = mContext.getResources().getDrawable(R.drawable.recycle_small);
        recycleSmall = createOverlay(requestedList, defaultMarker2);

        requestedList = PoiProviderRequest.getList(mContext, categories.toilet);
        
        Drawable defaultMarker3 = mContext.getResources().getDrawable(R.drawable.toilet);
        toilet = createOverlay(requestedList, defaultMarker3);

        requestedList = PoiProviderRequest.getList(mContext, categories.playground);
        
        Drawable defaultMarker4 = mContext.getResources().getDrawable(R.drawable.playground);
        playground = createOverlay(requestedList, defaultMarker4);

        requestedList = PoiProviderRequest.getList(mContext, categories.taxiStop);
        
        Drawable defaultMarker5 = mContext.getResources().getDrawable(R.drawable.taxi);
        taxiStop = createOverlay(requestedList, defaultMarker5);

        requestedList = PoiProviderRequest.getList(mContext, categories.recycleCenter);
        
        Drawable defaultMarker6 = mContext.getResources().getDrawable(R.drawable.recycle_center);
        recyleCenter = createOverlay(requestedList, defaultMarker6);

        
    }

    public List<Overlay> fillOverlayList(List<Overlay> overlayList) {
        if (PoiMap.LOG_D) {
            Log.d("Sam", "calling fillOverlayList");
        }
        pullData();
        
        if(libraryOverlay != null && libraryOverlay.size() != 0){
            overlayList.add(libraryOverlay);
        }
        if(cultureCenters != null && cultureCenters.size() != 0){
            overlayList.add(cultureCenters);
        }
        if(toilet  != null && toilet.size() != 0){
            overlayList.add(toilet);
        }
        if(playground  != null && playground.size() != 0){
            overlayList.add(playground);
        }
        if(recycleSmall != null && recycleSmall.size() != 0){
            overlayList.add(recycleSmall);
        }
        if(taxiStop  != null && taxiStop.size() != 0){
            overlayList.add(taxiStop);
        }
        if(recyleCenter  != null && recyleCenter.size() != 0){
            overlayList.add(recyleCenter);
        }
        
        return overlayList;
    }

    public List<Overlay> fillOverlaysWithFollowing(List<Overlay> overlayList,
            Set<categories> selectedPoiCategories) {
        if (PoiMap.LOG_D) {
            Log.d("Sam", "calling fillOverlaysWithfolloing");
        }

        overlayList.clear();
        for (categories cat : selectedPoiCategories) {
            if (cat == categories.culturalCenter && cultureCenters != null) {
                overlayList.add(cultureCenters);
            }
            if (cat == categories.library && libraryOverlay != null) {
                overlayList.add(libraryOverlay);
            }
            if (cat == categories.playground && playground != null) {
                overlayList.add(playground);
            }
            if (cat == categories.recycleCenter && recyleCenter != null) {
                overlayList.add(recyleCenter);
            }
            if (cat == categories.recycleSmall && recycleSmall != null) {
                overlayList.add(recycleSmall);
            }
            if (cat == categories.taxiStop && taxiStop != null) {
                overlayList.add(taxiStop);
            }
            if (cat == categories.toilet && toilet != null) {
                overlayList.add(toilet);
            }
        }

        if (LocationListenerImpl.currentPositionOverlay != null) {
            overlayList.add(LocationListenerImpl.currentPositionOverlay);
        }
        
        return overlayList;
    }

    private PoiOverlay createOverlay(Poi[] pois,
            Drawable defaultMarker) {
        
        PoiOverlay overlay = null;
        
        if(pois != null && pois.length != 0){
            overlay = new PoiOverlay(defaultMarker, mContext);
            
            for (int i = 0; i < pois.length; i++) {
                if (pois[i] != null) {
                    
                    overlay.addOverlay(convertPoiToOverlay(pois[i]));
                } else {
                    if (PoiMap.LOG_D) {
                        Log.d("Sam", "Element " + String.valueOf(i) + " is null!");
                    }
                }
            }
            overlay.populateOverlays();            
        }
        
        
        
        return overlay;
    }

    private static OverlayItem convertPoiToOverlay(Poi poi) {

        if (poi == null) {
            Log.w("OverlayManager:convertPoiToOverlay", "Poi object is null!");
        }
        if (poi.street == null) {
            Log.w("OverlayManager::convertPoiToOverlay", "streetname is null!");
        }
        if (poi.name == null) {
            Log.w("OverlayManager::convertPoiToOverlay", "poiName[de] is null!");
        }
        if (PoiMap.LOG_D) {
            Log.d("Sam", "convertPoiToOverlay called, name " + poi.name + ", lon " + String.valueOf(poi.longitude)
                    + " lat " + String.valueOf(poi.latitude));
        }
        GeoPoint point = new GeoPoint((int)((poi.latitude * 1E6)), (int)((poi.longitude * 1E6)));

        // build the snippet string
        StringBuilder snippetBuilder = new StringBuilder();
        snippetBuilder.append(poi.street);
        if (poi.streetnumber != null) {
            snippetBuilder.append(" ").append(poi.streetnumber).append("\n");
        }
        snippetBuilder.append(poi.zipcode).append(" ").append(poi.city).append("\n");
        if (poi.description != null && !poi.description.equals("")) {
            snippetBuilder.append("\n").append(poi.description);
        }

        OverlayItem item = new OverlayItem(point, poi.name, snippetBuilder.toString());

        return item;
    }
}
